package com.boss.core.form;

import com.boss.core.BossConstants;
import com.boss.core.common.Target;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import hudson.model.Hudson;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Superclass for Boss forms
 * @author Momin Noor Khan
 */
public class BossForm {
    private StaplerRequest request;
    private JSONObject jsonForm;
    private String urlParams;
    private Map<String, String> urlMap;
    private String jobUrl;
    private String startedBy;
    private String project;
    private String availableJob;
    private String task;
    private JSONObject jsonTaskSection;
    private String release;

    //Getters and Setters
    public StaplerRequest getRequest() {
        return request;
    }

    public void setReq(StaplerRequest request) {
        this.request = request;
    }

    public JSONObject getJsonForm() {
        return jsonForm;
    }

    public void setJsonForm(JSONObject jsonForm) {
        this.jsonForm = jsonForm;
    }

    public Map<String, String> getUrlMap() {
        return urlMap;
    }

    public void setUrlMap(Map<String, String> urlMap) {
        this.urlMap = urlMap;
    }

    public String getUrlParams() {
        return urlParams;
    }

    public void setUrlParams(String urlParams) {
        this.urlParams = urlParams;
    }

    public String getJobUrl() {
        return jobUrl;
    }

    public void setJobUrl(String jobUrl) {
        this.jobUrl = jobUrl;
    }

    public String getStartedBy() {
        return startedBy;
    }

    public void setStartedBy(String startedBy) {
        this.startedBy = startedBy;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public String getAvailableJob() {
        return availableJob;
    }

    public void setAvailableJob(String availableJob) {
        this.availableJob = availableJob;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public JSONObject getJsonTaskSection() {
        return jsonTaskSection;
    }

    public void setJsonTaskSection(JSONObject jsonTaskSection) {
        this.jsonTaskSection = jsonTaskSection;
    }

    public String getRelease() {
        return release;
    }

    public void setRelease(String release) {
        this.release = release;
    }

//    public BossForm() {
//        jsonForm = new JSONObject();
//        urlParams = "";
//        jobUrl = "";
//        startedBy = "";
//        project = "";
//        availableJob = "";
//        task = "";
//        jsonTaskSection = new JSONObject();
//        release = "";
//    }

    public BossForm(StaplerRequest req, JSONObject jForm, String proj) {
        request = req;
        jsonForm = jForm;
        startedBy = BossUtil.getStartedBy();
        project = proj;
        availableJob = BossUtil.findAvailableJob(project);
        release = BossUtil.getStringValueFromJson(jForm, BossConstants.KEY_RELEASE);
        jsonTaskSection = BossUtil.getObjectFromJson(jForm, BossConstants.KEY_TASK);
        task = BossUtil.getStringValueFromJson(jsonTaskSection, BossConstants.KEY_STAPLER_CLASS);
        urlMap = new HashMap<String, String>();
    }

    /**
     * Prepare the url parameter map which would be used later on while constructing job url
     */
    public void prepareUrlParamMap() {
        urlMap.put(BossConstants.URL_PARAM_STARTED_BY, startedBy);
        urlMap.put(BossConstants.URL_PARAM_RELEASE, release);
        urlMap.put(BossConstants.URL_PARAM_TASK, task);
    }

    /**
     * Construct the job url for submission
     * @param availableJob
     * @param urlParams
     * @return
     */
    public String getJobUrl(String availableJob, String urlParams) {
        return Hudson.getInstance().getRootUrl() +
                "job/" + availableJob +
                "/buildWithParameters?" +
                "delay=" + BossGlobalUtil.getJobDelay() +
                urlParams;
    }

    /**
     * Submit the form: Http Post is performed on the constructed job url
     * @throws ServletException
     * @throws IOException
     */
    public void submit() throws ServletException, IOException {
        prepareUrlParamMap();
        urlParams = BossFormHelper.getDecoratedQueryString(urlMap);
        jobUrl = getJobUrl(availableJob, urlParams);
        BossFormHelper.postToUrl(request, jobUrl);
    }

    /**
     * Check if the form is valid
     * @return
     */
    public boolean isValid() {
        return false;  //default value is false
    }
}
